import AddUser from "./controller/AddUser";
import mongodb from './common/MongoDBConn';

let Run = function(){
    let addUser = new AddUser();
    let argv : string[] = process.argv.splice(2);
    console.log(argv);
    let username = argv[0];
    let password = argv[1];
    let checkString = (param:string):boolean=>{
        if(!param){
            return false;
        }
        if(param.length > 0){
            return true;
        }
        return false;
    };
    if(!checkString(username)){
        console.log("用户名 不能为空");
        process.exit(1);
    }
    if(!checkString(password)){
        console.log("密码 不能为空");
        process.exit(1);
    }


    addUser.addOne(username, password).then((ret:boolean)=>{
        if(ret){
            console.log("用户已添加");
        }
        else{
            console.log("添加用户出错 (已存在）");
        }
        process.exit(1);
    });
};

let dbConn = new mongodb();
dbConn.connectDB(function(){
    Run();
},  function(){
    console.error('[FATAL ERROR] mongodb should run before server start.');
});


